package com.example.bighomework;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.bighomework.activity.LoginActivity;
import com.example.bighomework.activity.RegisterActivity;
import com.example.bighomework.activity.WeatherActivity;
import com.example.bighomework.adapter.ViewPagerAdapter;
import com.example.bighomework.fragment.FocusFragment;
import com.example.bighomework.fragment.MusicFragment;
import com.example.bighomework.fragment.NearbyFragment;
import com.example.bighomework.fragment.NewsFragment;
import com.example.bighomework.fragment.RecommendFragment;
import com.example.bighomework.fragment.VideosFragment;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.example.bighomework.databinding.ActivityMainBinding;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
   private DrawerLayout drawerLayout;
   private ImageView menu,weather;
//   分页步骤 1.tabLayout viewPager2 2.添加适配器 3.主页实现
   private TabLayout tabLayout;
   private ViewPager2 viewPager2;
   private ViewPagerAdapter viewPagerAdapter;
   String[] titleArray = new String[]{"电子琴","天气","新闻","视频","音乐","图片"};
   private NavigationView navigationView;
   private TextView login,register;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = findViewById(R.id.drawer_layout);
        menu = findViewById(R.id.drawerLayout);
        sharedPreferences = getSharedPreferences("user_data", MODE_PRIVATE);

        drawerLayout = findViewById(R.id.drawer_layout);
        tabLayout = findViewById(R.id.tabLayout);
        viewPager2 = findViewById(R.id.viewPager);
        navigationView = findViewById(R.id.nav_view);
        View headerLayout =navigationView.getHeaderView(0);
        login = headerLayout.findViewById(R.id.login);
        weather = headerLayout.findViewById(R.id.weather);
        register = headerLayout.findViewById(R.id.register);
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new FocusFragment());
        fragmentList.add(new RecommendFragment());
        fragmentList.add(new NewsFragment());
        fragmentList.add(new VideosFragment());
        fragmentList.add(new MusicFragment());
        fragmentList.add(new NearbyFragment());
        viewPagerAdapter = new ViewPagerAdapter(this,fragmentList, titleArray);
        viewPager2.setAdapter(viewPagerAdapter);
        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tabLayout,viewPager2,true,((tab, position) -> {
            tab.setText(viewPagerAdapter.getTitle(position));
        }));
        tabLayoutMediator.attach();

        menu.setOnClickListener(view -> drawerLayout.open());

        login.setText(sharedPreferences.getString("username",""));
//登录注册页跳转
        login.setOnClickListener(view -> {
            Intent intentLogin = new Intent(this, LoginActivity.class);
            startActivity(intentLogin);
        });
        register.setOnClickListener(view -> {
            Intent intentRegister = new Intent(this, RegisterActivity.class);
            startActivity(intentRegister);
        });
    }

}